package com.example.wsh.worddemo;


import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.wsh.worddemo.DatabaseFile.AssetsDatabaseManager;
import com.example.wsh.worddemo.Notebook_Fragment.NotebookFragment;
import com.example.wsh.worddemo.Users_Fragment.UsersFragment;
import com.example.wsh.worddemo.Word_Fragment.WordFragment;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragments;


//    声明导航栏布局
    private LinearLayout TabWord;
    private LinearLayout TabNotebook;
    private LinearLayout TabUsers;

    //    声明下方导航栏按钮
    private ImageButton IWord;
    private ImageButton INotebook;
    private ImageButton IUsers;
//    声明三个TextView
    private TextView text_word;
    private TextView text_notebook;
    private TextView text_users;
//    声明碎片
    private WordFragment wordFragment;
    private NotebookFragment notebookFragment;
    private UsersFragment usersFragment;

//    声明事件监听
    private View.OnClickListener onClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setColor(MainActivity.this,140);
        ActionBar actionBar  = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }
        AssetsDatabaseManager.initManager(getApplication());
        viewPager = findViewById(R.id.viewpager);

        //初始化
        initViews();
        initData();
        iniEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AssetsDatabaseManager.closeAllDatabase();
    }

    //为所有碎片设置事件监听
    private void iniEvent() {
        TabWord.setOnClickListener(onClickListener);
        TabNotebook.setOnClickListener(onClickListener);
        TabUsers.setOnClickListener(onClickListener);
    }

    //初始化Main的界面
    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(wordFragment);
        fragments.add(notebookFragment);
        fragments.add(usersFragment);

        //初始化碎片的适配器
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };

        //适配器给viewPager
        viewPager.setAdapter(fragmentPagerAdapter);

        //给viewPaper设置事件监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                viewPager.setCurrentItem(i);
                resetimages();
                selectTab(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    //当碎片页发生变化，下方的图标随之变化
    private void selectTab(int i) {
        switch (i){
            case 0:
                IWord.setImageResource(R.drawable.select_word);
                text_word.setTextColor(Color.parseColor("#00aeff"));
                text_notebook.setTextColor(Color.parseColor("#3f4247"));
                text_users.setTextColor(Color.parseColor("#3f4247"));
                break;
            case 1:
                INotebook.setImageResource(R.drawable.select_notebook);
                text_notebook.setTextColor(Color.parseColor("#00aeff"));
                text_word.setTextColor(Color.parseColor("#3f4247"));
                text_users.setTextColor(Color.parseColor("#3f4247"));
                break;
            case 2:
                IUsers.setImageResource(R.drawable.select_users);
                text_users.setTextColor(Color.parseColor("#00aeff"));
                text_notebook.setTextColor(Color.parseColor("#3f4247"));
                text_word.setTextColor(Color.parseColor("#3f4247"));
                break;
        }

        viewPager.setCurrentItem(i);
    }

    //设置三个按钮为黑色
    private void resetimages() {
        IWord.setImageResource(R.drawable.no_select_word);
        INotebook.setImageResource(R.drawable.no_select_notebook);
        IUsers.setImageResource(R.drawable.no_select_users);
    }

    //初始化View控件
    private void initViews() {
        //实例化布局
        TabWord = findViewById(R.id.tab_word);
        TabNotebook = findViewById(R.id.tab_notebook);
        TabUsers = findViewById(R.id.tab_users);

        //实例化TexiView
        text_word     = findViewById(R.id.text_word);
        text_notebook = findViewById(R.id.text_notebook);
        text_users    = findViewById(R.id.text_users);

        //实例化按钮
        IWord = findViewById(R.id.image_word);
        INotebook = findViewById(R.id.image_notebook);
        IUsers = findViewById(R.id.image_users);

        //实例化碎片
        wordFragment = new WordFragment();
        notebookFragment = new NotebookFragment();
        usersFragment = new UsersFragment();

        //初始化第一个按钮为亮
        IWord.setImageResource(R.drawable.select_word);
        text_word.setTextColor(Color.parseColor("#00aeff"));

        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = 0;
                switch (v.getId()){
                    case R.id.tab_word:
                        index = 0;
                        break;
                    case R.id.tab_notebook:
                        index = 1;
                        break;
                    case R.id.tab_users:
                        index = 2;
                        break;
                }
                viewPager.setCurrentItem(index);
            }
        };
    }
}
